﻿/* QF classbuilder for Enlib */
using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Logging;

#region SYS_RoleUser实体类
public class SYS_RoleUser
{
	private string m_RoleID;
	private string m_UserName;
	
	public SYS_RoleUser ()
	{
		m_RoleID="";
		m_UserName="";
	}
	
	public string RoleID
	{
		get{return m_RoleID;}
		set{m_RoleID=value;}
	}
	public string UserName
	{
		get{return m_UserName;}
		set{m_UserName=value;}
	}
}
#endregion

#region SYS_RoleUser管理类
public partial class LogicLayer
{
	public bool AddSYS_RoleUser(SYS_RoleUser theSYS_RoleUser)
	{
		try
		{
			Database db = DatabaseFactory.CreateDatabase("connStr");
			string sql = "insert into SYS_RoleUser (RoleID,UserName) values (@RoleID,@UserName)";
			System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql);
			
			db.AddInParameter(cmd, "@RoleID", DbType.String, theSYS_RoleUser.RoleID);
			db.AddInParameter(cmd, "@UserName", DbType.String, theSYS_RoleUser.UserName);
			
			db.ExecuteNonQuery(cmd);
			return true;
		}
		catch(Exception ex)
		{
			Logger.Write("AddSYS_RoleUser:" + ex.Message );
			return false;
		}
	}
	
	public SYS_RoleUser GetSYS_RoleUserFromDataReader(IDataReader dataReader)
	{
		try
		{
			SYS_RoleUser result = new SYS_RoleUser();
			
			if (dataReader["RoleID"] != null && dataReader["RoleID"] != System.DBNull.Value )
				result.RoleID = (string)dataReader["RoleID"];
			if (dataReader["UserName"] != null && dataReader["UserName"] != System.DBNull.Value )
				result.UserName = (string)dataReader["UserName"];
			
			return result;
		}
		catch(Exception ex)
		{
			Logger.Write("GetReaderSYS_RoleUser:" + ex.Message);
			return null;
		}
	}
	
	public SYS_RoleUser GetSYS_RoleUser(string RoleID,string UserName)
	{
		try
		{
			Database db = DatabaseFactory.CreateDatabase("connStr");
			string sql = "select * From SYS_RoleUser where RoleID=@RoleID and UserName=@UserName";
			
			System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql);
			db.AddInParameter(cmd, "@RoleID", DbType.String, RoleID);
			db.AddInParameter(cmd, "@UserName", DbType.String, UserName);
			
			SYS_RoleUser result = null;
			using (IDataReader dataReader = db.ExecuteReader(cmd))
			{
				if (dataReader.Read())
				{
					result = GetSYS_RoleUserFromDataReader( dataReader );
				}
			}
			return result;
		}
		catch(Exception ex)
		{
			Logger.Write("GetSYS_RoleUser:" + ex.Message);
			return null;
		}
	}
	
	public bool DelSYS_RoleUser(string RoleID,string UserName)
	{
		try
		{
			Database db = DatabaseFactory.CreateDatabase("connStr");
			string sql = "delete From SYS_RoleUser where RoleID=@RoleID and UserName=@UserName";
			
			System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql);
			db.AddInParameter(cmd, "@RoleID", DbType.String, RoleID);
			db.AddInParameter(cmd, "@UserName", DbType.String, UserName);
			
			db.ExecuteNonQuery(cmd);
			return true;
		}
		catch(Exception ex)
		{
			Logger.Write("DelSYS_RoleUser:" + ex.Message);
			return false;
		}
	}
	
	public bool UpdateSYS_RoleUser(SYS_RoleUser theSYS_RoleUser)
	{
		try
		{
			Database db = DatabaseFactory.CreateDatabase("connStr");
			string sql = "update SYS_RoleUser set  where RoleID=@RoleID and UserName=@UserName";
			System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql);
			
			db.AddInParameter(cmd, "@RoleID", DbType.String, theSYS_RoleUser.RoleID);
			db.AddInParameter(cmd, "@UserName", DbType.String, theSYS_RoleUser.UserName);
			
			db.ExecuteNonQuery(cmd);
			return true;
		}
		catch(Exception ex)
		{
			Logger.Write("UpdateSYS_RoleUser:" + ex.Message);
			return false;
		}
	}
	
	public List<SYS_RoleUser> ListSYS_RoleUser()
	{
		try
		{
			Database db = DatabaseFactory.CreateDatabase("connStr");
			string sql = "select * from SYS_RoleUser";
			System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql);
			
			 List<SYS_RoleUser> result = new  List<SYS_RoleUser>();
			using (IDataReader dataReader = db.ExecuteReader(cmd))
			{
				while (dataReader.Read())
				{
					SYS_RoleUser obj = GetSYS_RoleUserFromDataReader( dataReader );
					if(obj != null)
						result.Add(obj);
				}
			}
			return result;
		}
		catch(Exception ex)
		{
			Logger.Write("ListSYS_RoleUser:" + ex.Message);
			return null;
		}
	}
	
}
#endregion